<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="780px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="审核" prop="check_status">
                <el-radio-group v-model="model.check_status" @change="changeCheckStatus">
                    <el-radio :label="2">审核通过</el-radio>
                    <el-radio :label="3">审核不通过</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="spu" v-if="model.check_status == 2" key="spu_key">
                <el-input v-model.trim="model.spu" placeholder="请输入SPU" clearable></el-input>
            </el-form-item>
            <el-form-item label="原因" v-else-if="model.check_status == 3" key="remarks_key">
                <el-input type="textarea" v-model.trim="model.remarks" placeholder="请输入原因" :autosize="{ minRows: 3, maxRows: 6 }" clearable style="width: 600px;"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { queryProductNewDetail, productNewAudit } from '@/api/product'

export default {
    name: 'ProductNewAuditDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: undefined,
                check_status: 2,
                spu: undefined,
                remarks: undefined,
            },
            rules: {},
        }
    },
    computed: {
        title() {
            return `审核产品上新`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.model.id = val.id
                    // this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {},
    methods: {
        changeCheckStatus() {
            if (this.model.check_status == 2) {
                this.model.remarks = undefined
            } else if (this.model.check_status == 3) {
                this.model.spu = undefined
            }
        },
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            queryProductNewDetail(params)
                .then(res => {
                    // this.model = Object.assign({}, this.model, res.data)
                    this.model.id = res.data.id
                    this.model.check_status = res.data.check_status
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                productNewAudit(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
